package com.dmx.platform.action;

import java.util.Date;

import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.web.struts2.action.CommonQueryAction;
import com.dmx.platform.entity.TOrder;
import com.dmx.platform.service.Message;
import com.dmx.platform.service.NotifyService;
import com.dmx.platform.service.OrderService;

/**
 *@author: Beni Yang
 *@date:2012-8-18
 *@function:
 */
@SuppressWarnings("serial")
public class OrderAction extends CommonQueryAction {

	private TOrder order;
	private OrderService orderService;

	private String[] ids;
	private String id;
	private String q_password;
	private String operateId;

	// actions begin

	public String execute() throws Exception {
		super.busiId = "searchNewOrder";
		if (ViewConstant.QUERY.equals(super.act)) {
			doQuery();
		} else if (ViewConstant.REQUERY.equals(super.act)) {
			doQuery();
		}
		// confirm the order
		else if (ViewConstant.ADDINPUT.equals(super.act)) {
			this.pageState = ViewConstant.ADDSTATUS;
			return doAddInput();
		}
		// submit and confirm order
		else if (ViewConstant.ADDSUBMIT.equals(super.act)) {
			return doAddSubmit();
		}

		else if (ViewConstant.EDITINPUT.equals(super.act)) {
			return doEditInput();
		}

		else if (ViewConstant.EDITSUBMIT.equals(super.act)) {
			return doEditSubmit();

		} else {
			super.execute();
		}
		return SUCCESS;
	}

	public String doAddInput() {

		return ViewConstant.ADDINPUT;
	}

	public String doAddSubmit() throws Exception {

		try {
			order = orderService.getOrderByPassword(q_password, Integer.parseInt(this.getBusiDataId()));
			if (order != null) {
				log.info("Current order [" + order.getOrderNum() + "] status is " + order.getStatus());
				if (order.getStatus().equals(TOrder.STATUS_NEW) 
						|| order.getStatus().equals(TOrder.STATUS_ALLOCATED) 
						|| order.getStatus().equals(TOrder.STATUS_CANCELED)) {
					order.setConfirmTime(new Date());
					order.setStatus(TOrder.STATUS_CONFIRMED);
					if (orderService.saveOrUpdateOrder(order)) {
						log.info("Order [" + order.getOrderNum() + "] has been confirmed by password " + q_password);
						NotifyService.outingQ.offer(new Message(order.getUserPhone()
								,order.getBaseId()+"/"+this.getCurrOperator().getLogin()
								,Message.TYPE_CONFIRMED
								,order.getOrderName()));
					} else {
						log.error("Order [" + order.getOrderNum() + "] confirmed failed by password " + q_password);
					}
				}

			}

		} catch (Exception e) {
			log.error("Find order by password failed. " + e);
		}
		if (this.order == null) {
			this.setErrorInfo("订单密码错误，无法查到对应的订单信息，请输入正确的密码重试！");
			this.setPageState("add");
		} else {
			this.setPageState("view");
		}
		return ViewConstant.ADDINPUT;
	}

	public String doEditInput() {
		if (this.id != null) {
			order = orderService.getOrderById(id);
		}
		return ViewConstant.EDITINPUT;
	}

	public String doEditSubmit() throws Exception {
		String promptMsg = "";
		try {
			TOrder t = orderService.getOrderById(String.valueOf(order.getId()));
			t.setAllocateRoom(order.getAllocateRoom());
			t.setAllocateTime(new Date());
			t.setAllocateOpt(order.getAllocateOpt());
			if (operateId.equals("1")) {
				promptMsg = order.getId() + "号订单分配成功，系统将会发送一条‘分配消息’给用户，到时可根据用户的‘订单密码’来确认该订单！";
				t.setStatus(TOrder.STATUS_ALLOCATED);
				if (orderService.saveOrUpdateOrder(t)) {
					setSuccessinfo(promptMsg);
					NotifyService.outingQ.offer(new Message(t.getUserPhone()
							,t.getBaseId()+"/"+this.getCurrOperator().getLogin()
							,Message.TYPE_ALLOCATED
							,t.getOrderName()));
				} else {
					setErrorInfo(promptMsg);
				}
			} else {
				promptMsg = order.getId() + "号订单拒绝成功，系统将会发送一条‘拒绝消息’给用户，该订单可在‘历史订单’中查看详情！";
				t.setStatus(TOrder.STATUS_REFUSED);
				if (orderService.saveOrUpdateOrder(t)) {
					setSuccessinfo(promptMsg);
					NotifyService.outingQ.offer(new Message(t.getUserPhone()
							,t.getBaseId()+"/"+this.getCurrOperator().getLogin()
							,Message.TYPE_REFUSED
							,t.getOrderName()));
				} else {
					setErrorInfo(promptMsg);
				}
			}
			
		} catch (Exception e) {
			log.error("operate order failed [" + order.getId() + "] " + e);
			this.setErrorInfo(order.getId() + "号订单操作失败，请刷新后重试！");
		}
		doQuery();
		return SUCCESS;
	}

	public String doDelete() throws Exception {
		// if (this.id != null) {
		// infoService.deleteBaseInfoById(id);
		// }
		// setSuccessinfo(getText("info.deleteSuccess"));
		return super.doQuery();
	}

	public TOrder getOrder() {
		return order;
	}

	public void setOrder(TOrder order) {
		this.order = order;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getQ_password() {
		return q_password;
	}

	public void setQ_password(String q_password) {
		this.q_password = q_password;
	}

	public String getOperateId() {
		return operateId;
	}

	public void setOperateId(String operateId) {
		this.operateId = operateId;
	}

}